//
//  CartCountControlView.m
//  Trunk
//
//  Created by xiaowu on 2017/6/16.
//
//

#import "CartCountControlView.h"

static CGFloat itemWidth = 32;

@interface CartCountControlView ()<UITextFieldDelegate>

@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;

@end

@implementation CartCountControlView

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:self.textField];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self setup];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setup];
        
//        [self.textField setLeftView:self.leftButton];
//        [self.textField setLeftViewMode:UITextFieldViewModeAlways];
//        [self.textField setRightView:self.rightButton];
//        [self.textField setRightViewMode:UITextFieldViewModeAlways];
    }
    return self;
}

- (void)setup {
    self.backgroundColor = [UIColor grayF9Color];
//    self.layer.borderWidth = 0.5;
//    self.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.clipsToBounds = YES;
    
    self.layer.cornerRadius = 2;
    
    [self addSubview:self.textField];
    [self addSubview:self.leftButton];
    [self addSubview:self.rightButton];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:self.textField];

}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.textField.frame = CGRectMake(itemWidth, 0, itemWidth, self.height);
    self.leftButton.frame = CGRectMake(0, 0, itemWidth, self.height);
    self.rightButton.frame = CGRectMake(2 * itemWidth, 0, itemWidth, self.height);
//    self.leftButton.frame = CGRectMake(0, 0, itemWidth, self.height);
//    self.rightButton.frame = CGRectMake(0, 0, itemWidth, self.height);
}

- (void)leftButtonClick:(UIButton *)sender {
    if (self.textField.text.integerValue > 1) {
        self.textField.text = [NSString stringWithFormat:@"%ld", (long)(self.textField.text.integerValue - 1)];
    }
    if (self.block) {
        self.block(self.textField.text, NO);
    }
    NSLog(@"left press");
}

- (void)rightButtonClick:(UIButton *)sender {
    if (self.textField.text.integerValue < self.maxCount) {
        [self.textField setText:[NSString stringWithFormat:@"%ld", (long)(self.textField.text.integerValue + 1)]];
        if (self.block) {
            self.block(self.textField.text, NO);
        }
    } else {
        if (self.block) {
            self.block(self.textField.text, YES);
        }
    }
    
    NSLog(@"right press");
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *textStr = [textField.text stringByReplacingCharactersInRange:range withString:string];

//    if (textStr.length > 3 && range.length!=1){
//        textField.text = [textStr substringToIndex:3];
//        return NO;
//    }
    
//    if (self.block) {
//        self.block(self.textField.text);
//    }
    
    return YES;
}

#pragma mark - Observer

- (void)textFieldTextDidChange:(NSNotification *)info {
    UITextField *textField = (UITextField *)info.object;
    if (textField == self.textField) {
        if ([textField.text isEqualToString:@""]) {
            [textField setText:@"0"];
        }
        if (self.maxCount > 0) {
            NSInteger count = [textField.text integerValue];
            if (count > self.maxCount) {
                count = self.maxCount;
                [textField setText:[NSString stringWithFormat:@"%ld", (long)count]];
                
                if (self.block) {
                    self.block(self.textField.text, YES);
                }
            } else {
                [textField setText:[NSString stringWithFormat:@"%ld", (long)count]];
                
                if (self.block) {
                    self.block(self.textField.text, NO);
                }
            }
        } else {
            if (self.block) {
                self.block(self.textField.text, NO);
            }
        }
        
    }
    
}

#pragma mark - getters
- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
//        _textField.layer.borderWidth = 0.5;
//        _textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _textField.textAlignment = NSTextAlignmentCenter;
        _textField.font = TextNormalFont14;
        _textField.keyboardType = UIKeyboardTypeNumberPad;
        _textField.delegate = self;
    }
    return _textField;
}

- (UIButton *)leftButton {
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftButton.frame = CGRectMake(0, 0, itemWidth, itemWidth);
        [_leftButton addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_leftButton setImage:[UIImage imageNamed:@"reduce"] forState:UIControlStateNormal];
    }
    return _leftButton;
}

- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.frame = CGRectMake(0, 0, itemWidth, itemWidth);
        [_rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_rightButton setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    }
    return _rightButton;
}

@end
